﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Threading;

public partial class UserControl_ControlChiNhanh : System.Web.UI.UserControl
{
    string str = webapp4U.Configuration.CurrentConnectionString;
    ClassExecute.ClassExecute dt;
    string error = "";

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            if (ViewState["flag"] == null)
            {
                ViewState["flag"] = false;
            }

            //nằm trong thread
            ThreadStart thr1 = new ThreadStart(LoadLoaiChiNhanh);
            ThreadStart thr2 = new ThreadStart(LoadThuocChiNhanh);
            ThreadStart thr3 = new ThreadStart(LoadGrid);

            Thread tr1 = new Thread(thr1);
            Thread tr2 = new Thread(thr2);
            Thread tr3 = new Thread(thr3);

            tr1.Start();
            tr2.Start();
            tr3.Start();

            tr1.Join();
            tr2.Join();
            tr3.Join();
            //nằm trong thread
        }
    }
    protected void imgBtnAdd_Click(object sender, ImageClickEventArgs e)
    {
        txtMaSo.ReadOnly = false;
        txtMaSo.Text = "";
        txtName.Text = "";
        txtDiaChi.Text = "";
        txtPhone.Text = "";
        ViewState["flag"] = true;
    }
    protected void imgBtnSave_Click(object sender, ImageClickEventArgs e)
    {
         dt = new ClassExecute.ClassExecute();
        int i=0;
        try
        {
            if ((bool)ViewState["flag"])
            {
                //insert
                i = dt.ExecuteSQl_int(str, "k_insertBranchlist", CommandType.StoredProcedure, ref error,
                    new SqlParameter("@Sitecode", txtMaSo.Text),
                    new SqlParameter("@sitename", txtName.Text),
                    new SqlParameter("@Address", txtDiaChi.Text),
                    new SqlParameter("@Tel", txtPhone.Text),
                    new SqlParameter("@parentSite", ddlChiNhanh.SelectedValue.ToString()),
                    new SqlParameter("@SiteType", int.Parse(ddlType.SelectedValue.ToString())),
                    new SqlParameter("@Active",chbDangHoatDong.Checked));
                if (i == 1)
                {
                    txtMaSo.ReadOnly = true;
                    uscMsgBox1.AddMessage("Khai báo chi nhánh mới thành công", uscMsgBox.enmMessageType.Success);
                    LoadGrid();

                }
                else
                {
                    uscMsgBox1.AddMessage("Hệ thống đang bảo trì", uscMsgBox.enmMessageType.Error);
                }
                ViewState["flag"] = false;
               
            }
            else
            {
                //update
                i = dt.ExecuteSQl_int(str, "k_updateBranchlist", CommandType.StoredProcedure, ref error,
                   new SqlParameter("@Sitecode", txtMaSo.Text),
                   new SqlParameter("@sitename", txtName.Text),
                   new SqlParameter("@Address", txtDiaChi.Text),
                   new SqlParameter("@Tel", txtPhone.Text),
                   new SqlParameter("@parentSite", ddlChiNhanh.SelectedValue.ToString()),
                   new SqlParameter("@SiteType", int.Parse(ddlType.SelectedValue.ToString())),
                   new SqlParameter("@Active", chbDangHoatDong.Checked));
                if (i == 1)
                {
                    uscMsgBox1.AddMessage("Cập nhật thông tin thành công", uscMsgBox.enmMessageType.Success);
                    LoadGrid();
                }
                else
                {
                    uscMsgBox1.AddMessage("Hệ thống đang bảo trì", uscMsgBox.enmMessageType.Error);
                }

            }
        }
        catch (Exception ex)
        {

            string method = System.Reflection.MethodBase.GetCurrentMethod().Name;
            dt.WriteErrorLog(Server.MapPath(webapp4U.Globals.LogUrl()), ex.Message, method);
        }    
    }
    protected void imgBtnFind_Click(object sender, ImageClickEventArgs e)
    {
        TabContainer1.ActiveTabIndex = 1;
    }
    protected void btnSearch_Click(object sender, EventArgs e)
    {

    }
    protected void grvBranch_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        grvBranch.PageIndex = e.NewPageIndex;
        LoadGrid();
    }

    protected void grvBranch_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        dt = new ClassExecute.ClassExecute();
        try
        {
            if (e.CommandName == "chon")
            {
                GridViewRow grv = ((Control)e.CommandSource).NamingContainer as GridViewRow;
                Label lblMaSo = (Label)grv.FindControl("lblMaSo");

                DataTable db = dt.Get_Data_DataTable(str, ref error, "k_getDetailBranchList", CommandType.StoredProcedure,
                    new SqlParameter("@Sitecode", lblMaSo.Text));

                txtMaSo.Text = db.Rows[0]["Sitecode"].ToString();
                txtName.Text = db.Rows[0]["sitename"].ToString();
                txtDiaChi.Text = db.Rows[0]["Address"].ToString();
                txtPhone.Text = db.Rows[0]["Tel"].ToString();
                try
                {
                    ddlType.SelectedValue = db.Rows[0]["SiteType"].ToString();
                }
                catch
                {

                }
                try
                {
                    ddlChiNhanh.SelectedValue = db.Rows[0]["parentSite"].ToString();
                }
                catch
                {

                }
                chbDangHoatDong.Checked = bool.Parse(db.Rows[0]["Active"].ToString());

                txtMaSo.ReadOnly = true;
                ViewState["flag"] = false;
                TabContainer1.ActiveTabIndex = 0;
            }
        }
        catch (Exception ex)
        {

            string method = System.Reflection.MethodBase.GetCurrentMethod().Name;
            dt.WriteErrorLog(Server.MapPath(webapp4U.Globals.LogUrl()), ex.Message, method);
        }    
    }
    void LoadLoaiChiNhanh()
    {
        dt = new ClassExecute.ClassExecute();
        try
        {

            DataTable db = dt.Get_Data_DataTable(str, ref error, "k_getBranchType", CommandType.StoredProcedure);
            dt.BindDropDownList(db, ddlType, "BranchTypeID", "BranchTypeDesc");
        }
        catch (Exception ex)
        {

            string method = System.Reflection.MethodBase.GetCurrentMethod().Name;
            dt.WriteErrorLog(Server.MapPath(webapp4U.Globals.LogUrl()), ex.Message, method);
        }
    }
    void LoadThuocChiNhanh()
    {
        dt = new ClassExecute.ClassExecute();
        try
        {

            DataTable db = dt.Get_Data_DataTable(str, ref error, "k_getBranchListDemand", CommandType.StoredProcedure);
            dt.BindDropDownList(db, ddlType, "Sitecode", "sitename");
        }
        catch (Exception ex)
        {

            string method = System.Reflection.MethodBase.GetCurrentMethod().Name;
            dt.WriteErrorLog(Server.MapPath(webapp4U.Globals.LogUrl()), ex.Message, method);
        }
    }
    void LoadGrid()
    {
        dt = new ClassExecute.ClassExecute();
        try
        {
            DataTable db = dt.Get_Data_DataTable(str, ref error, "k_showbranchlist", CommandType.StoredProcedure);
            grvBranch.DataSource = db;
            grvBranch.DataBind();
            if (db.Rows.Count == 0)
            {
                uscMsgBox1.AddMessage("Không tìm thấy kết quả", uscMsgBox.enmMessageType.Attention);
            }
        }
        catch (Exception ex)
        {

            string method = System.Reflection.MethodBase.GetCurrentMethod().Name;
            dt.WriteErrorLog(Server.MapPath(webapp4U.Globals.LogUrl()), ex.Message, method);
        }
    }
  
}